package io.renren.modules.com.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.com.entity.ComContractEntity;
import io.renren.modules.com.service.ComContractService;
import io.renren.modules.sys.controller.AbstractController;

/**
 * 
 * 
 * @author pfp
 * @email
 * @date 2017-07-16 09:34:53
 */
@Controller
@RequestMapping("comcontract")
public class ComContractController extends AbstractController {
	@Autowired
	private ComContractService comContractService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("com:contract:list")
	@ResponseBody
	public R list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);

		List<ComContractEntity> comContractList = comContractService.queryList(query);
		int total = comContractService.queryTotal(query);

		PageUtils pageUtil = new PageUtils(comContractList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}

	@RequestMapping("/select")
	@ResponseBody
	public R select() {
		Map<String, Object> params = new HashMap<String, Object>();
		// 查询列表数据
		Query query = new Query(params);
		List<ComContractEntity> contractList = comContractService.queryList(query);

		return R.ok().put("contractList", contractList);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{contractId}")
	@RequiresPermissions("com:contract:info")
	@ResponseBody
	public R info(@PathVariable("contractId") Long contractId) {
		ComContractEntity comContract = comContractService.queryObject(contractId);

		return R.ok().put("comContract", comContract);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("com:contract:save")
	@ResponseBody
	public R save(@RequestBody ComContractEntity comContract) {
		ValidatorUtils.validateEntity(comContract);
		comContract.setCreateTime(new Date());
		comContract.setCreateUser(getUserId());
		R r = comContractService.save(comContract);

		return r;
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("com:contract:update")
	@ResponseBody
	public R update(@RequestBody ComContractEntity comContract) {
		ValidatorUtils.validateEntity(comContract);
		R r = comContractService.update(comContract);

		return r;
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("com:contract:delete")
	@ResponseBody
	public R delete(@RequestBody Long[] contractIds) {
		comContractService.deleteBatch(contractIds);

		return R.ok();
	}

	@RequestMapping("/updateSupple")
	@ResponseBody
	public R updateSupple(@RequestBody ComContractEntity comContract) {
		R r = comContractService.updateSupple(comContract);
		return r;
	}

	@RequestMapping("/endContract")
	@ResponseBody
	public R endContract(@RequestBody ComContractEntity comContract) {
		R r = comContractService.endContract(comContract);
		return r;
	}

	@RequestMapping("/contractExport")
	public void contractExport(@RequestParam Map<String, Object> params, HttpServletResponse response) {

		try {
			response.setContentType("octets/stream");
			response.addHeader("Content-Disposition",
					"attachment;filename=" + new String("合同列表".getBytes("utf-8"), "iso8859-1") + ".xls");
			OutputStream out = response.getOutputStream();
			// 查询列表数据
			List<ComContractEntity> contractList = comContractService.queryList(params);
			comContractService.contractExport("合同列表", contractList, out);
			out.flush();
			out.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
